﻿using JGSY.DotNetty.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JGSY.NotNetty.ControllerBus
{
    public class ControllerBusImpl : IControllerBus
    {
        private readonly IServiceProvider _ServiceProvider;
        private readonly ControllerHelper _controllerHelper;
        public ControllerBusImpl(IServiceProvider serviceProvider, ControllerHelper controllerHelper)
        {
            _ServiceProvider = serviceProvider;
            _controllerHelper = controllerHelper;
        }
        public BaseController GetController(string key)
        {
            Type type = _controllerHelper.GetController(key);
            if (type == null)
            {
                throw new DotNettyServerException("未找到控制器");
            }
            object controller = _ServiceProvider.GetService(type);

            if (controller == null)
            {
                throw new DotNettyServerException($"未找到控制器{type.FullName}");
            }
            if (!(controller is BaseController baseController))
            {
                throw new DotNettyServerException($"未找到控制器{type.FullName}");
            }
            return baseController;
        }
    }
}
